//DevNETCoder
//CopyRight By DevNET Group
using System;
using System.Data;
using System.Data.SqlClient;
using System.Collections.Generic;
using STT.Entities;
using STT.DataAccess;
 
namespace STT.Business
{
    public class BTbTinNhan
    {
//-----------------------------------------------------------//
        public static DataTable SelectAll()
        {
            DataTable all = SqlHelper.ExecuteData(CommandType.StoredProcedure, "TbTinNhan_SelectAll", null);
            return all;
        }
        public static DataTable SelectTop(int Top)
        {
            SqlParameter[] pr = new SqlParameter[1];
            pr[0] = new SqlParameter(@"Top", Top);
            DataTable all = SqlHelper.ExecuteData(CommandType.StoredProcedure, "TbTinNhan_SelectTop", pr);
            return all;
        }
//-----------------------------------------------------------//
        public static void Insert(ETbTinNhan OTbTinNhan)
        {
            SqlParameter[] pr = new SqlParameter[4];
            pr[0] = new SqlParameter(@"TinNhan", OTbTinNhan.TinNhan);
            pr[1] = new SqlParameter(@"MaHV", OTbTinNhan.MaHV);
            pr[2] = new SqlParameter(@"NgayGui", OTbTinNhan.NgayGui);
            pr[3] = new SqlParameter(@"NguoiGui", OTbTinNhan.NguoiGui);
            SqlHelper.ExecuteNonQuery(CommandType.StoredProcedure,"TbTinNhan_Insert", pr);
        }
//-----------------------------------------------------------//
        public static void Update(ETbTinNhan OTbTinNhan)
        {
            SqlParameter[] pr = new SqlParameter[5];
            pr[0] = new SqlParameter(@"MaTN", OTbTinNhan.MaTN);
            pr[1] = new SqlParameter(@"TinNhan", OTbTinNhan.TinNhan);
            pr[2] = new SqlParameter(@"MaHV", OTbTinNhan.MaHV);
            pr[3] = new SqlParameter(@"NgayGui", OTbTinNhan.NgayGui);
            pr[4] = new SqlParameter(@"NguoiGui", OTbTinNhan.NguoiGui);
            SqlHelper.ExecuteNonQuery(CommandType.StoredProcedure,"TbTinNhan_Update", pr);
        }
//-----------------------------------------------------------//
        public static void Delete(int MaTN)
        {
            SqlParameter[] pr = new SqlParameter[1];
            pr[0] = new SqlParameter(@"MaTN",MaTN);
            SqlHelper.ExecuteNonQuery(CommandType.StoredProcedure, "TbTinNhan_Delete", pr);
        }
//-----------------------------------------------------------//
        public static ETbTinNhan SelectByID(int MaTN)
        {
            ETbTinNhan OTbTinNhan = new ETbTinNhan();
            SqlParameter[] pr = new SqlParameter[1];
            pr[0] = new SqlParameter(@"MaTN",MaTN);
            IDataReader idr = SqlHelper.ExecuteReader(CommandType.StoredProcedure,"TbTinNhan_SelectByID", pr);
            if (idr.Read())
              OTbTinNhan = GetOneTbTinNhan(idr);
            idr.Close();
            idr.Dispose();
            return OTbTinNhan;
        }
//-----------------------------------------------------------//
        private static ETbTinNhan GetOneTbTinNhan(IDataReader idr)
       {
            ETbTinNhan OTbTinNhan = new ETbTinNhan();
                if (idr["MaTN"] != DBNull.Value)
                    OTbTinNhan.MaTN = (int)idr["MaTN"];
                if (idr["TinNhan"] != DBNull.Value)
                    OTbTinNhan.TinNhan = (string)idr["TinNhan"];
                if (idr["MaHV"] != DBNull.Value)
                    OTbTinNhan.MaHV = (string)idr["MaHV"];
                if (idr["NgayGui"] != DBNull.Value)
                    OTbTinNhan.NgayGui = (DateTime)idr["NgayGui"];
                if (idr["NguoiGui"] != DBNull.Value)
                    OTbTinNhan.NguoiGui = (string)idr["NguoiGui"];
            return OTbTinNhan;
       }
//-----------------------------------------------------------//
        public static bool TestByID(int MaTN)
        {
            SqlParameter[] pr = new SqlParameter[2];
            pr[0] = new SqlParameter(@"MaTN",MaTN);
            pr[1] = new SqlParameter(@"TestID",SqlDbType.Bit);
            pr[1].Direction = ParameterDirection.Output;
            SqlHelper.ExecuteNonQuery(CommandType.StoredProcedure,"TbTinNhan_TestByID", pr);
            return Convert.ToBoolean(pr[1].Value);
        }
//-----------------------------------------------------------//
        public static List<ETbTinNhan> ListAll()
        {
            List<ETbTinNhan> list = new List<ETbTinNhan>();
            IDataReader idr = SqlHelper.ExecuteReader(CommandType.StoredProcedure, "TbTinNhan_SelectAll", null);
            while (idr.Read())
              list.Add(GetOneTbTinNhan(idr));
            if (idr.IsClosed == false)
            {
               idr.Close();
               idr.Dispose();
            }
            return list;
        }

        public static List<ETbTinNhan> ListTop(int Top)
        {
            SqlParameter[] pr = new SqlParameter[1];
            pr[0] = new SqlParameter(@"Top", Top);
            List<ETbTinNhan> list = new List<ETbTinNhan>();
            IDataReader idr = SqlHelper.ExecuteReader(CommandType.StoredProcedure, "TbTinNhan_SelectAll", pr);
            while (idr.Read())
              list.Add(GetOneTbTinNhan(idr));
            if (idr.IsClosed == false)
            {
               idr.Close();
               idr.Dispose();
            }
            return list;
        }
    }
 
}